<?php
namespace app\bancard\controller;
use think\Db;
use app\common\lib\Dir;
use app\common\lib\Backup;
class Database extends Common {
	protected $db;
	
	public function __construct(){
	    parent::__construct();
		$this->config=array(
		    'path'     => './backup/',//数据库备份路径
		    'part'     => 20971520,//数据库备份卷大小
		    'compress' => 0,//数据库备份文件是否启用压缩 0不压缩 1 压缩
		    'level'    => 9 //数据库备份文件压缩级别 1普通 4 一般  9最高
		);
		$this->db= new Backup($this->config);
	}
	public function index(){
		return $this->fetch('index',['list'=>$this->db->dataList()]);
	}
	public function backups(){
		$data = request()->param();
		$file=['name'=>date('YmdHis'),'part'=>1];
		$tables=$data['data'];
		foreach ($tables as $value) {
		    $start= $this->db->setFile($file)->backup($value, 0);
		}
		return show($start,"备份完成",200);
	}
	public function datalist(){
		return $this->fetch();
	}
	//备份列表
	public function restore(){
	    $data = Dir::tree($this->config['path']);
		return showTable(0, 'OK',count($data), $data, 200);
	}
	public function lists(){
		 $list = $this->db->dataList();
		 return showTable(0, 'OK',100, $list, 200);
		
	}
	//删除备份
	public function dele(){
		$data = request()->param();
		$url = $data['path'];
		$res = Dir::del($url);
		if($res){
			return show(1,"删除完成",200);
		}else{
			return show(0,"删除失败",200);
		}
		
	}
	//下载文件
	public function downfile(){
		$data = request()->param();
		$url = $data['path'];
		if (file_exists($url)) {
		    ob_end_clean();
		    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
		    header('Content-Description: File Transfer');
		    header('Content-Type: application/octet-stream');
		    header('Content-Length: ' . filesize($url));
		    header('Content-Disposition: attachment; filename=' . basename($url));
		    readfile($url);
		} else {
		    return show(1,"文件不存在",200);
		}
	}
	public function repair(){
		$data = request()->param();
		$tables=$data['data'];
		$data = $this->db->repair($tables);
		return show(0,"修复完成",200);
	}
}